package v2

import (
	"net/http"
)

type Server interface {
	Route(method string, pattern string, handlerFunc func(c *Context))
	Start(address string) error
}

type sdkHttpServer struct {
	Name    string
	handler *HandlerBasedOnMap
}

func (s *sdkHttpServer) Route(method string, pattern string, handlerFunc func(c *Context)) {
	key := s.handler.Key(method, pattern)
	s.handler.handlers[key] = handlerFunc
}

func (s *sdkHttpServer) Start(adder string) error {
	http.Handle("/", s.handler)
	return http.ListenAndServe(adder, nil)
}

func NewSdkHttpServer(name string) Server {
	return &sdkHttpServer{
		Name: name,
		handler: &HandlerBasedOnMap{
			handlers: make(map[string]func(c *Context), 10),
		},
	}
}
